<div class="layuimini-main">
	<div class="layui-form layuimini-form" lay-filter="form-data">
		<div class="layui-form-item">
			<label class="layui-form-label required">仓库类型</label>
			<div class="layui-input-block">
				<select name="type" id="type" lay-filter="type">
					<option value="">请选择</option>
					<option value="GitLab">GitLab</option>
					<option value="Codeup">Codeup(阿里云)</option>
				</select>
			</div>
		</div>
		
		<!-- GitLab start -->
		<div class="layui-form-item" style="display: none" id="urlDiv">
			<label class="layui-form-label required">仓库地址</label>
			<div class="layui-input-block">
				<input type="text" name="gitLab.url" placeholder="如：https://jihulab.com" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authTypeDiv">
			<label class="layui-form-label required">认证方式</label>
			<div class="layui-input-block">
				<select name="gitLab.authType" lay-filter="authType" id="authType">
					<option value="">请选择</option>
					<option value="1">令牌认证</option>
					<option value="2">账号认证</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authTokenDiv">
			<label class="layui-form-label required">认证令牌</label>
			<div class="layui-input-block">
				<input type="text" name="gitLab.authToken" placeholder="请输入认证令牌" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authNameDiv">
			<label class="layui-form-label required">认证名称</label>
			<div class="layui-input-block">
				<input type="text" name="gitLab.authName" placeholder="请输入认证名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="authPasswordDiv">
			<label class="layui-form-label required">认证密码</label>
			<div class="layui-input-block">
				<input type="password" name="gitLab.authPassword" placeholder="请输入认证密码" autocomplete="off" class="layui-input">
			</div>
		</div>
		<!-- GitLab end -->
		
		<!-- Codeup start -->
		<div class="layui-form-item" style="display: none" id="organizationIdDiv">
			<label class="layui-form-label required">企业标识</label>
			<div class="layui-input-block">
				<input type="text" name="codeup.organizationId" placeholder="如：D4soa4847f4d9ebeb9efceaO" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="accessKeyDiv">
			<label class="layui-form-label required">AccessKey</label>
			<div class="layui-input-block">
				<input type="text" name="codeup.accessKey" placeholder="如：ANUI5tP2pKxpalUyesse54qL" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item" style="display: none" id="accessKeySecretDiv">
			<label class="layui-form-label required">AccessKeySecret</label>
			<div class="layui-input-block">
				<input type="text" name="codeup.accessKeySecret" placeholder="如：WllZ49UqOlalrygMW4Kq5LjsqRqDv3" autocomplete="off" class="layui-input">
			</div>
		</div>
		<!-- Codeup end -->
		
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
			</div>
		</div>
	</div>
<div>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

		$.ajax({
			url: '/globalConfig/query',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({"itemTypes": [2]}),
			success: function(res){
				if(res.code != "000000"){
					layer.msg(res.message, {icon: 5, shift: 6});
					return false;
				}
				if(JSON.stringify(res.data) === '{}'){
					return false;
				}
				
				if(res.data.codeRepo.type == 'GitLab'){
					var gitLabData = res.data.codeRepo.gitLab;
					form.val("form-data", {
						"type": res.data.codeRepo.type
						,"gitLab.url": gitLabData.url
						,"gitLab.authType": gitLabData.authType
						,"gitLab.authToken": gitLabData.authToken
						,"gitLab.authName": gitLabData.authName
						,"gitLab.authPassword": gitLabData.authPassword
					});
					if(res.data.codeRepo.authType == 1){
						$("#authTokenDiv").show();
					}else if(res.data.codeRepo.authType == 2){
						$("#authNameDiv").show();
						$("#authPasswordDiv").show();
					}
					initType('GitLab', $, gitLabData.authType);
				}else if(res.data.codeRepo.type == 'Codeup'){
					var codeupData = res.data.codeRepo.codeup;
					form.val("form-data", {
						"type": res.data.codeRepo.type
						,"codeup.organizationId": codeupData.organizationId
						,"codeup.accessKey": codeupData.accessKey
						,"codeup.accessKeySecret": codeupData.accessKeySecret
					});
					initType('Codeup', $);
				}else{
					initType('GitLab', $);
				}
				
			},
			error: function(res){
				layer.msg(res.message, {icon: 5, shift: 6});
			}
		});
		
		form.render('select');
		
		initType('GitLab', $);
		
		form.on('select(type)', function(data) {
			initType(data.value, $, $("#authType").val());
		});
		
		//认证方式
		form.on('select(authType)', function(data) {
			initAuthType(data.value, $);
		});
		
        //监听提交
        form.on('submit(saveBtn)', function (data) {
			if(data.field.type == 'GitLab'){
			
				var fields = data.field;
				var allParams = {};
				var extendParams = {};
				for(var fieldName in fields){
					var fs = fieldName.split(".");
					if(fs.length > 1){
						extendParams[fs[1]] = fields[fieldName];
					}else{
						allParams[fieldName] = fields[fieldName];
					}
				}
				allParams['gitLab'] = extendParams;
			
				if(allParams.gitLab.url == ''){
					layer.msg('仓库地址不能为空', {icon: 5, shift: 6});
					return false;
				}
				if(allParams.gitLab.url.substr(0, 4) !== 'http'){
					layer.msg('仓库地址格式不正确', {icon: 5, shift: 6});
					return false;
				}
				//令牌认证
				if(allParams.gitLab.authType == 1){
					if(data.field.authToken == ''){
						layer.msg("认证令牌不能为空", {icon: 5, shift: 6});
						return false;
					}
					allParams.gitLab.authName = '';
					allParams.gitLab.authPassword = '';
				//账号认证
				}else if(allParams.gitLab.authType == 2){
					if(allParams.gitLab.authName == '' || allParams.gitLab.authPassword == ''){
						layer.msg("认证名称和密码不能为空", {icon: 5, shift: 6});
						return false;
					}
					allParams.gitLab.authToken = '';
				}
			}else if(data.field.type == 'Codeup'){
				var fields = data.field;
				var allParams = {};
				var extendParams = {};
				for(var fieldName in fields){
					var fs = fieldName.split(".");
					if(fs.length > 1){
						extendParams[fs[1]] = fields[fieldName];
					}else{
						allParams[fieldName] = fields[fieldName];
					}
				}
				allParams['codeup'] = extendParams;
				
				if(allParams.codeup.organizationId == ''){
					layer.msg('企业标识不能为空', {icon: 5, shift: 6});
					return false;
				}
				if(allParams.codeup.accessKey == ''){
					layer.msg('AccessKey不能为空', {icon: 5, shift: 6});
					return false;
				}
				if(allParams.codeup.accessKeySecret == ''){
					layer.msg('AccessKeySecret不能为空', {icon: 5, shift: 6});
					return false;
				}
			}
			
			//数据类型：代码仓库配置
			allParams.itemType = 2;
			$.ajax({
				url: '/globalConfig/codeRepo/addOrUpdate',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify(allParams),
				success: function(res){
					if(res.code != "000000"){
						layer.msg(res.message, {icon: 5, shift: 6});
						return false;
					}	
					layer.msg("保存成功", {
						icon: 1,
						time: 700,
						shade: 0.01,
						shadeClose: false
					});
				},
				error: function(res){
					layer.msg(res.message, {icon: 5, shift: 6});
				}
			});

            return false;
        });

    });
	
	function initType(type, $, authType){
		if(type == 'GitLab'){
			$("#urlDiv").show();
			$("#authTypeDiv").show();
			$("#organizationIdDiv").hide();
			$("#accessKeyDiv").hide();
			$("#accessKeySecretDiv").hide();
			initAuthType(authType, $);
		}else if(type == 'Codeup'){
			$("#urlDiv").hide();
			$("#authTypeDiv").hide();
			$("#authTokenDiv").hide();
			$("#authNameDiv").hide();
			$("#authPasswordDiv").hide();
			$("#organizationIdDiv").show();
			$("#accessKeyDiv").show();
			$("#accessKeySecretDiv").show();
		}
	}
	
	function initAuthType(authType, $){
		//令牌认证
		if(authType == 1){
			$("#authTokenDiv").show();
			$("#authNameDiv").hide();
			$("#authPasswordDiv").hide();
		//账号认证
		}else if(authType == 2){
			$("#authTokenDiv").hide();
			$("#authNameDiv").show();
			$("#authPasswordDiv").show();
		}else{
			$("#authTokenDiv").hide();
			$("#authNameDiv").hide();
			$("#authPasswordDiv").hide();
		}
	}
</script>